<script setup>
import { fetchRoleList, postDeleteRole } from '@/api/roleApi'
import { onMounted, ref } from 'vue'
import AddRole from './components/AddRole.vue'
import { ElMessage } from 'element-plus'
import logger from '@/utils/logger'

// 对话框可见状态
const dialogVisible = ref(false)

// 当前编辑的角色ID
const currentRoleId = ref(null)

// 打开添加角色对话框
const openAddRoleDialog = () => {
  currentRoleId.value = null
  dialogVisible.value = true
}

// 打开编辑角色对话框
const openEditRoleDialog = (row) => {
  currentRoleId.value = row.dealerRoleId
  dialogVisible.value = true
}

// 关闭对话框
const closeDialog = () => {
  dialogVisible.value = false
}

// 角色列表数据
const tableData = ref([])

// 加载状态
const loading = ref(false)

// 获取角色列表
const handleFetchRoleList = async () => {
  try {
    loading.value = true
    const res = await fetchRoleList()
    tableData.value = res.data || []
  } catch (error) {
    logger.error('获取角色列表失败', error)
    ElMessage.error('获取角色列表失败')
  } finally {
    loading.value = false
  }
}

// 删除角色
const handleDeleteRole = async (row) => {
  try {
    loading.value = true
    const res = await postDeleteRole(row.dealerRoleId)
    if (res.code === 200) {
      ElMessage.success('删除成功')
    } else {
      ElMessage.error(res.msg)
    }
  } catch (error) {
    logger.error('删除角色失败', error)
    ElMessage.error('删除角色失败')
  } finally {
    await handleFetchRoleList()
    loading.value = false
  }
}

// 组件挂载时获取数据
onMounted(async () => {
  await handleFetchRoleList()
})
</script>

<template>
  <!-- 使用单个对话框，根据currentRoleId判断是添加还是编辑 -->
  <el-dialog
    v-model="dialogVisible"
    :title="currentRoleId ? '编辑角色' : '添加角色'"
    width="50%"
    destroy-on-close
    align-center
  >
    <AddRole :dealer-role-id="currentRoleId" @success="handleFetchRoleList" @close="closeDialog" />
  </el-dialog>

  <div class="m-container">
    <div class="controller-container">
      <div class="controller-item">
        <el-button type="primary" @click="openAddRoleDialog">
          <template #icon>
            <el-icon><LineMdPlus /></el-icon>
          </template>
          添加角色
        </el-button>
      </div>
    </div>
    <div class="table-container">
      <el-table v-loading="loading" :data="tableData" style="width: 100%; height: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="roleName" label="角色名称" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="openEditRoleDialog(row)">
              <template #icon>
                <el-icon><LineMdEdit /></el-icon>
              </template>
              编辑
            </el-button>

            <el-popconfirm
              placement="top-start"
              confirm-button-text="确定"
              cancel-button-text="取消"
              :title="`确定要删除角色吗？`"
              width="220"
              confirm-button-type="danger"
              @confirm="handleDeleteRole(row)"
            >
              <template #reference>
                <el-button type="danger" size="small">
                  <template #icon>
                    <el-icon><LineMdTrash /></el-icon>
                  </template>
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.m-container {
  display: flex;
  flex-direction: column;
  height: 100%;

  .controller-container {
    border-radius: 10px;
    background-color: #fff;
    padding: 10px;
    margin-bottom: 10px;
    .controller-item {
      display: flex;
      align-items: center;
      gap: 10px;
    }
  }

  .table-container {
    width: 100%;
    height: 100%;
    flex: 1;
    background-color: #fff;
    border-radius: 10px;
    padding: 10px;
  }
}
</style>
